CentOS 7
Sponsored Link

Cgroups : Create Control Groups
2017/08/30
 
Create Control Groups.
[1] Confirm current valid subsystems on the System.
[root@dlp ~]#
lssubsys

cpuset
cpu,cpuacct
memory
devices
freezer
net_cls,net_prio
blkio
perf_event
hugetlb
pids
[2] For example, Create a Control Group under the CPU subsystem.
# show current settings for cpu subsystem

[root@dlp ~]#
cgget -g cpu /

/:
cpu.rt_period_us: 1000000
cpu.rt_runtime_us: 950000
cpu.stat: nr_periods 0
        nr_throttled 0
        throttled_time 0
cpu.cfs_period_us: 100000
cpu.cfs_quota_us: -1
cpu.shares: 1024

# create a Control group named [cpu_quota-70]

[root@dlp ~]#
cgcreate -g cpu:/cpu_quota-70
# confirm

[root@dlp ~]#
cgget -g cpu /cpu_quota-70

/cpu_quota-70:
cpu.rt_period_us: 1000000
cpu.rt_runtime_us: 0
cpu.stat: nr_periods 0
        nr_throttled 0
        throttled_time 0
cpu.cfs_period_us: 100000
cpu.cfs_quota_us: -1
cpu.shares: 1024

# change [cpu.cfs_quota_us]'s parameter to [70000μs]

# possible to access CPU for [70000μs] time per [100000μs] which is the [cpu.cfs_period_us]'s parameter

[root@dlp ~]#
cgset -r cpu.cfs_quota_us=70000 cpu_quota-70

[root@dlp ~]#
cgget -g cpu /cpu_quota-70

/cpu_quota-70:
cpu.rt_period_us: 1000000
cpu.rt_runtime_us: 0
cpu.stat: nr_periods 0
        nr_throttled 0
        throttled_time 0
cpu.cfs_period_us: 100000
cpu.cfs_quota_us: 70000
cpu.shares: 1024

# setting is created under the specified subsystem like follows

[root@dlp ~]#
ll /sys/fs/cgroup/cpu/cpu_quota-70

total 0
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cgroup.clone_children
--w--w----. 1 root root 0 Aug 31 19:51 cgroup.event_control
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cgroup.procs
-r--r--r--. 1 root root 0 Aug 31 19:51 cpuacct.stat
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cpuacct.usage
-r--r--r--. 1 root root 0 Aug 31 19:51 cpuacct.usage_percpu
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cpu.cfs_period_us
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cpu.cfs_quota_us
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cpu.rt_period_us
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cpu.rt_runtime_us
-rw-rw-r--. 1 root root 0 Aug 31 19:51 cpu.shares
-r--r--r--. 1 root root 0 Aug 31 19:51 cpu.stat
-rw-rw-r--. 1 root root 0 Aug 31 19:51 notify_on_release
-rw-rw-r--. 1 root root 0 Aug 31 19:51 tasks
[3] For adding specific process in the Control Group above, set like follows.
# run a job which places stress on CPU for a test

[root@dlp ~]#
yes > /dev/null &

[1] 1116
# show processes on [top] command

[root@dlp ~]#
top -b -n 1 -p 1116

top - 13:33:47 up 2 min,  1 user,  load average: 0.30, 0.08, 0.03
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  7.4 us,  1.2 sy,  0.0 ni, 91.3 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  4046872 total,  3811756 free,   111808 used,   123308 buff/cache
KiB Swap:  3145724 total,  3145724 free,        0 used.  3736592 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1116 root      20   0  107896    608    512 R 100.0  0.0   0:16.90 yes

# show CPU usage per CPU cores (average of CPU#0 is now 100% by the test job above)

[root@dlp ~]#
mpstat -P ALL 1 1 | grep ^Average

Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all   49.75    0.00    0.50    0.00    0.00    0.00    0.00    0.00    0.00   49.75
Average:       0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
Average:       1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

# add the test proccess to [cpu_quota-70] group

[root@dlp ~]#
cgclassify -g cpu:cpu_quota-70 1116
# specified process is added in [tasks] of Control Group like follows

[root@dlp ~]#
cat /sys/fs/cgroup/cpu/cpu_quota-70/tasks

1116
# just turned to 70% of CPU average by Control Group like follows

[root@dlp ~]# mpstat -P ALL 1 1 | grep ^Average
Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all   34.67    0.00    0.50    0.00    0.00    0.00    0.00    0.00    0.00   64.82
Average:       0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
Average:       1   70.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   30.00
[4] If you'd like to remove a Control Group, set like follows.
[root@dlp ~]#
cgdelete cpu:cpu_quota-70
# confirm setting

[root@dlp ~]#
lscgroup -g cpu:/cpu_quota-70

[5] The Control Groups that is added by [cgcreate] command is cleared when System restarts, so if you'd like to set it persistence, configure it in setting file like follows.
[root@dlp ~]#
vi /etc/cgconfig.d/cpu_quota-70.conf
# create new setting file

group cpu_quota-70 {
    cpu {
        cpu.cfs_quota_us="70000";
    }
}
 
Tweet